#include "../vcd-common-api/vcd-common.h"
#include "vcd-message-dialog.h"
#include "../spicy.h"

static GMainLoop *mainloop = NULL;

typedef struct _inter_ctx{
    message_diag_context* ctx;
    GtkWidget *window;
}inter_ctx;

typedef struct _timer_ctx{
    GtkWidget* widget;
    int interval;
    
    gint timerid;
    GtkWidget *msg_box;
}timer_ctx;

static void press_yes(inter_ctx* t);
static void press_no(inter_ctx* t);

static void press_yes(inter_ctx* t)
{
    message_diag_context* ctx = t->ctx;
    if (ctx && ctx->btn_yes)
        ctx->btn_yes(ctx->user_data);

    if (t->window)
        gtk_widget_destroy(t->window);
    G_SAFE_FREE(t);
}
static void press_no(inter_ctx* t)
{
    message_diag_context* ctx = t->ctx;
    if (ctx && ctx->btn_no)
        ctx->btn_no(ctx->user_data);
        
    if (t->window)
        gtk_widget_destroy(t->window);
    G_SAFE_FREE(t);
}

static gboolean timer_callback(gpointer data){
    timer_ctx* ctx = (timer_ctx*)data;
    GtkWidget* btn = (GtkWidget*)ctx->widget;

    if(ctx->interval <= 0){
        g_signal_emit_by_name(btn,"clicked");
        g_free(ctx);
        return FALSE;
    }

    printf("timer_callback :%p,visible:%d\n", btn,gtk_widget_is_visible(btn));

    char text[50] = {0};
    sprintf(text,"关闭 (%d)",ctx->interval--);
    gtk_button_set_label(btn,text);
    return TRUE;
}

int vcd_message_dialog(message_diag_context* ctx)
{
    g_return_val_if_fail(ctx != NULL, -1);

    GtkWidget *label, *label_pic, *message_window, *btn_yes, *btn_no, *fix, *scrolled_win;

    message_window = gtk_window_new(GTK_WINDOW_POPUP);
    gtk_window_set_keep_above(GTK_WINDOW(message_window), TRUE);
    gtk_window_set_modal((GtkWindow *)message_window, TRUE);
    gtk_window_set_position(GTK_WINDOW(message_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(message_window), 50);
    gtk_window_set_decorated(GTK_WINDOW(message_window), FALSE); //无边框
    gtk_window_set_transient_for(GTK_WINDOW(message_window), GTK_WINDOW(win_toplevel));

    char label_msg[255];
    if (ctx->info)
        snprintf(label_msg, sizeof(label_msg), "\n%s\n", ctx->info);
    else
        snprintf(label_msg, sizeof(label_msg), "\n%s\n", "message");

    M_DEBUG("dialog message = %s", label_msg);

    label  = gtk_label_new(label_msg);
    #if defined(__WIN32__)
        label_pic = gtk_image_new_from_file(vcd_common_get_absolute_img_path("wenhao.png"));
    #else
        label_pic = gtk_image_new_from_file(VCD_PIC_PATH"/wenhao.png");
    #endif
    gtk_widget_set_size_request(label_pic, 70, 70);

    btn_yes = gtk_button_new_with_label("确定");
    btn_no = gtk_button_new_with_label("取消");
    gtk_button_set_relief(GTK_BUTTON(btn_no), GTK_RELIEF_NONE);
    gtk_button_set_relief(GTK_BUTTON(btn_yes), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btn_yes, 110, 0);
    gtk_widget_set_size_request(btn_no, 110, 0);

    scrolled_win = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_size_request(scrolled_win, 250, 0);
    gtk_container_add(GTK_CONTAINER(scrolled_win), label);

    fix = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(message_window), fix);

    gtk_fixed_put((GtkFixed *)fix, label_pic, 85, 20);
    gtk_fixed_put((GtkFixed *)fix, scrolled_win, 0, 100);
    gtk_fixed_put((GtkFixed *)fix, btn_yes, 0, 155);
    gtk_fixed_put((GtkFixed *)fix, btn_no,  140, 155);

    message_diag_context* ctx_cp = g_malloc(sizeof(message_diag_context));
    g_return_val_if_fail(ctx_cp != NULL, -1);
    *ctx_cp = *ctx;

    inter_ctx* t = g_malloc0(sizeof(inter_ctx));
    if (t == NULL) {
        LOG_ERROR("malloc inter_ctx error");
        G_SAFE_FREE(ctx_cp);
        return -1;
    }

    t->ctx = ctx_cp;
    t->window = message_window;

    g_signal_connect_swapped(G_OBJECT(btn_yes), "clicked", G_CALLBACK(press_yes), t);
    g_signal_connect_swapped(G_OBJECT(btn_no), "clicked", G_CALLBACK(press_no), t);

    /*            CSS               */
    //set_img(label_pic, "label", "wenhao.png");
    set_css(btn_yes, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btn_yes, "button:hover{border-radius: 0px;background: #3070a9}");
    set_css(btn_no, "button{color: white;background: #5cb85c;border: 1px solid rgb(57, 132, 57);border-radius: 0px}");
    set_css(btn_no, "button:hover{border-radius:0px;background: #449d44;}");
    set_css(message_window, "window{border-radius:0px;background-color: rgb(230, 230, 230)}");
    /*            CSS                 */
    gtk_widget_show_all(message_window);
    gtk_window_set_position(GTK_WINDOW(message_window), GTK_WIN_POS_CENTER_ALWAYS);//居中

    return 0;
}

static void _gtk_widget_destroy(timer_ctx *ctx){
    g_return_if_fail(ctx != NULL);
    g_return_if_fail(ctx->timerid != NULL);
    g_return_if_fail(ctx->msg_box != NULL);
    
    g_source_remove(ctx->timerid);
    g_main_loop_quit(mainloop);
    gtk_widget_destroy(ctx->msg_box);
}

void message_box(const char *information)
{
    M_DEBUG("message box");
    GtkWidget *label, *label_pic, *window_message_box, *btn_ok, *fix, *scrolled_win;
    window_message_box = gtk_window_new(GTK_WINDOW_POPUP);
    gtk_window_set_modal((GtkWindow *)window_message_box, TRUE);
    gtk_window_set_keep_above(GTK_WINDOW(window_message_box), TRUE);
    gtk_window_set_position(GTK_WINDOW(window_message_box), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(window_message_box), 50);
    gtk_window_set_decorated(GTK_WINDOW(window_message_box), FALSE);//无边框

    fix = gtk_fixed_new();

    char tmp_info[120];
    snprintf(tmp_info,sizeof(tmp_info),"%s", information);
    label = gtk_label_new(tmp_info);

    btn_ok = gtk_button_new_with_label("关闭");
    gtk_button_set_relief(GTK_BUTTON(btn_ok), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btn_ok, 150, 0);

    #if defined(__WIN32__)
        label_pic = gtk_image_new_from_file(vcd_common_get_absolute_img_path("wenhao.png"));
    #else
        label_pic = gtk_image_new_from_file(VCD_PIC_PATH"/wenhao.png");    gtk_widget_set_size_request(label_pic, 70, 70);
    #endif
    scrolled_win  = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_size_request(scrolled_win, 250, 0);
    gtk_container_add(GTK_CONTAINER(scrolled_win), label);

    gtk_fixed_put((GtkFixed *)fix, label_pic, 85, 20);
    gtk_fixed_put((GtkFixed *)fix, scrolled_win, 0, 100);
    gtk_fixed_put((GtkFixed *)fix, btn_ok, 50, 155);

    gtk_container_add(GTK_CONTAINER(window_message_box), fix);

    // add timer
    timer_ctx *ctx = g_malloc0(sizeof(timer_ctx));
    ctx->widget = btn_ok;
    ctx->interval = 5;
    ctx->timerid = g_timeout_add_seconds(1, timer_callback, ctx);
    ctx->msg_box = window_message_box;

    g_signal_connect_swapped(G_OBJECT(btn_ok), "clicked", G_CALLBACK(_gtk_widget_destroy), ctx);
    /*            CSS                  */
    //set_img(label_pic, "label", "gantanhao.png");
    set_css(window_message_box, "window{border-radius:0px;background-color: rgb(230, 230, 230)}");
    set_css(btn_ok, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btn_ok, "button:hover{border-radius:0px;background: #3070a9}");
    /*            CSS                 */
    gtk_widget_show_all(window_message_box);

    mainloop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(mainloop);
    g_main_loop_unref(mainloop);
}

void show_warning(GtkWidget *window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
            GTK_DIALOG_MODAL,
            GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK,
            "Unallowed operation");
    gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

GtkWidget * message_box_reconnect(const char* msg,button_callback func){

    GtkWidget *label, *label_pic, *window_message_box, *btn_ok, *fix, *scrolled_win;
    window_message_box = gtk_window_new(GTK_WINDOW_POPUP);
    gtk_window_set_modal((GtkWindow *)window_message_box, TRUE);
    gtk_window_set_keep_above(GTK_WINDOW(window_message_box), TRUE);
    gtk_window_set_position(GTK_WINDOW(window_message_box), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(window_message_box), 50);
    gtk_window_set_decorated(GTK_WINDOW(window_message_box), FALSE);//无边框

    fix = gtk_fixed_new();

    const char *tmp_info = msg;
    label = gtk_label_new(tmp_info);

    btn_ok = gtk_button_new_with_label("关闭");
    gtk_button_set_relief(GTK_BUTTON(btn_ok), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btn_ok, 150, 0);

    #if defined(__WIN32__)
        label_pic = gtk_image_new_from_file(vcd_common_get_absolute_img_path("wenhao.png"));
    #else
        label_pic = gtk_image_new_from_file(VCD_PIC_PATH"/wenhao.png");
    #endif
    gtk_widget_set_size_request(label_pic, 70, 70);

    scrolled_win  = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_size_request(scrolled_win, 250, 0);
    gtk_container_add(GTK_CONTAINER(scrolled_win), label);

    gtk_fixed_put((GtkFixed *)fix, label_pic, 85, 20);
    gtk_fixed_put((GtkFixed *)fix, scrolled_win, 0, 100);
    gtk_fixed_put((GtkFixed *)fix, btn_ok, 50, 155);

    gtk_container_add(GTK_CONTAINER(window_message_box), fix);

    if(func)
    g_signal_connect_swapped(G_OBJECT(btn_ok), "clicked", G_CALLBACK(func), window_message_box);

    /*            CSS                  */
    //set_img(label_pic, "label", "gantanhao.png");
    set_css(window_message_box, "window{border-radius:0px;background-color: rgb(230, 230, 230)}");
    set_css(btn_ok, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btn_ok, "button:hover{border-radius:0px;background: #3070a9}");
    /*            CSS                 */
    gtk_widget_show_all(window_message_box);
    
    return window_message_box;
}

static gboolean timer_callback_logining(gpointer data){
    timer_ctx* ctx = (timer_ctx*)data;
    GtkWidget* label = (GtkWidget*)ctx->widget;

    if(ctx->interval <= 0){
        gtk_widget_destroy(ctx->msg_box);
        g_free(ctx);
        return FALSE;
    }

    printf("timer_callback :%p,visible:%d\n", label,gtk_widget_is_visible(label));

    char text[50] = {0};
    sprintf(text,"正在登陆 (%d)",ctx->interval--);
    gtk_label_set_label(label,text);
    return TRUE;
}
GtkWidget*  vcd_message_dialog_logining(){
    GtkWidget *label, *window_message_box, *fix, *scrolled_win;
    window_message_box = gtk_window_new(GTK_WINDOW_POPUP);
    gtk_window_set_modal((GtkWindow *)window_message_box, TRUE);
    gtk_window_set_keep_above(GTK_WINDOW(window_message_box), TRUE);
    gtk_window_set_position(GTK_WINDOW(window_message_box), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(window_message_box), 50);
    gtk_window_set_decorated(GTK_WINDOW(window_message_box), FALSE);//无边框

    fix = gtk_fixed_new();

    GtkWidget *connect_spinner = gtk_spinner_new();
    gtk_widget_set_size_request(connect_spinner, 70, 70);
    gtk_spinner_start(GTK_SPINNER(connect_spinner));

    char tmp_info[120];
    snprintf(tmp_info,sizeof(tmp_info),"%s", "正在登录");
    label = gtk_label_new(tmp_info);

    scrolled_win  = gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_size_request(scrolled_win, 250, 0);
    gtk_container_add(GTK_CONTAINER(scrolled_win), label);

    gtk_fixed_put((GtkFixed *)fix, connect_spinner, 85, 20);
    gtk_fixed_put((GtkFixed *)fix, scrolled_win, 0, 100);

    gtk_container_add(GTK_CONTAINER(window_message_box), fix);

    set_css(window_message_box, "window{border-radius:0px;background-color: rgb(230, 230, 230)}");

    gtk_widget_show_all(window_message_box);

    return window_message_box;
}

static gboolean cancle_login(GtkWidget *message_box)
{
#ifdef __WIN32__
    check_is_process_exist("Vclient.exe", TRUE);
#else
    system("killall verystream");
#endif
    gtk_widget_destroy(message_box);
    gtk_main_quit();
}

GtkWidget* vcd_message_dialog_logining_with_gif(const char* path,button_callback func){
    GtkWidget *window_message_box, *fix;
    window_message_box = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    //gtk_window_set_modal((GtkWindow *)window_message_box, TRUE);
    //gtk_window_set_keep_above(GTK_WINDOW(window_message_box), TRUE);
    gtk_window_set_position(GTK_WINDOW(window_message_box), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_window_set_decorated(GTK_WINDOW(window_message_box), FALSE);//无边框

    GdkPixbufAnimation*anim_pixbuf = gdk_pixbuf_animation_new_from_file(path, NULL);
    GtkWidget *image = gtk_image_new_from_animation(GDK_PIXBUF_ANIMATION(anim_pixbuf));

    GtkWidget* btn_cacle = gtk_button_new_with_label("取消");
    gtk_button_set_relief(GTK_BUTTON(btn_cacle), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btn_cacle, 150, 0);

    fix = gtk_fixed_new();
    gtk_fixed_put((GtkFixed *)fix, image, 0, 0);
    gtk_fixed_put((GtkFixed *)fix, btn_cacle, 75, 200);

    gtk_container_add(GTK_CONTAINER(window_message_box), fix);

    if(func)
        g_signal_connect_swapped(G_OBJECT(btn_cacle), "clicked", G_CALLBACK(func), window_message_box);
    else
        g_signal_connect_swapped(G_OBJECT(btn_cacle), "clicked", G_CALLBACK(cancle_login), window_message_box);

    set_css(btn_cacle, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 3px}");
    set_css(btn_cacle, "button:hover{border-radius:5px;background: #3070a9}");
    gtk_widget_show_all(window_message_box);

    return window_message_box;
}




